package com.module.home.widget

import android.content.Context
import android.graphics.Color
import android.text.TextUtils
import android.util.AttributeSet
import android.view.Gravity
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.TranslateAnimation
import android.widget.TextSwitcher
import android.widget.TextView
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.callback.NavCallback
import com.common.app.core.RouterHub
import com.common.app.data.bean.KeyBundle
import com.common.app.data.bean.SearchHotWordBean
import com.common.app.helper.RouterHelper
import com.common.app.utls.EVENT_SEARCH_BAR_CLICK
import com.common.app.utls.FIELD_CURRENT_PAGE_NAME
import com.common.app.utls.TrackUtils
import com.common.base.utils.ToastUtils
import com.module.home.R
import org.json.JSONObject

class SearWordSwitcher @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : TextSwitcher(context, attrs) {
    private val searchList = ArrayList<SearchHotWordBean>()
    private var index = -1
    private val scrollRunnable = Runnable {
        scrollToNext()
    }
    private var delayMillis: Long = 5000
    private var from: String = "unknown"

    init {
        setFactory {
            val textView = TextView(context)
            textView.layoutParams = LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
            textView.setTextColor(Color.BLACK)
            textView.textSize = 14f
            textView.gravity = Gravity.CENTER_VERTICAL
            textView.setSingleLine()
            textView.ellipsize = TextUtils.TruncateAt.END
            textView
        }
        val inAnim = TranslateAnimation(
            Animation.RELATIVE_TO_SELF,
            0f,
            Animation.RELATIVE_TO_SELF,
            0f,
            Animation.RELATIVE_TO_SELF,
            1f,
            Animation.RELATIVE_TO_SELF,
            0f
        )
        inAnim.duration = 800
        val outAnim = TranslateAnimation(
            Animation.RELATIVE_TO_SELF,
            0f,
            Animation.RELATIVE_TO_SELF,
            0f,
            Animation.RELATIVE_TO_SELF,
            0f,
            Animation.RELATIVE_TO_SELF,
            -1f
        )
        outAnim.duration = 800
        inAnimation = inAnim
        outAnimation = outAnim
        setOnClickListener {
            reportSearchBarClick(from)
            RouterHelper.navigationPostcard(RouterHub.ROUTER_HOME_SEARCH_PRODUCT)
                .withParcelable(KeyBundle.BUNDLE_KEY, searchList.getOrNull(index))
                .navigation(context, object : NavCallback() {
                    override fun onArrival(postcard: Postcard?) {
                    }

                    override fun onLost(postcard: Postcard?) {
                        ToastUtils.showToast(R.string.common_not_page_ac_hint)
                    }
                })
        }
    }

    private fun reportSearchBarClick(from: String) {
        val properties = JSONObject()
        properties.put(FIELD_CURRENT_PAGE_NAME, from)
        TrackUtils.track(EVENT_SEARCH_BAR_CLICK, properties)
    }

    private fun scrollToNext() {
        stopAutoScroll()
        if (searchList.size == 0) {
            return
        }
        index++
        index %= searchList.size
        setText(searchList.getOrNull(index)?.Word)
        if (searchList.size > 1) {
            postDelayed(scrollRunnable, delayMillis)
        }
    }

    fun size(): Int = searchList.size

    fun setFrom(f: String) {
        if (f.isNotEmpty()) {
            this.from = f
        }
    }

    fun setWords(words: List<SearchHotWordBean>?) {
        stopAutoScroll()
        searchList.clear()
        words?.let {
            searchList.addAll(it)
        }
        if (searchList.size > 0) {
            index = -1
            scrollToNext()
        }
    }

    fun startAutoScroll() {
        scrollToNext()
    }

    fun stopAutoScroll() {
        removeCallbacks(scrollRunnable)
    }

    override fun onAttachedToWindow() {
        startAutoScroll()
        super.onAttachedToWindow()
    }

    override fun onDetachedFromWindow() {
        stopAutoScroll()
        super.onDetachedFromWindow()
    }
}